package com.atguigu.test6;

/**
 * @author 路飞
 * @date 2024/4/6 14:59
 * @description 找出数组中一个值，使其左侧值的加和等于右侧值的加和，
 * 例如：1,2,5,3,2,4,2，结果为：第4个值
 */
public class Test6 {
    public static void main(String[] args) {
        int[] arr = {1,2,5,3,1,3,2,2,2,4,2};
        System.out.println("中间值的索引为：" + findIndex(arr));
    }

    public static int findIndex(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int left = i - 1;
            int leftSum = 0;
            int right = i + 1;
            int rightSum = 0;
            for (int j = 0; j <= left; j++) {
                leftSum += arr[j];
            }
            for (int k = right; k < arr.length; k++) {
                rightSum += arr[k];
            }
            if (leftSum == rightSum) {
                return i;
            }
        }
        return -1;
    }
}
